﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using Game_Fly.Code.AuxiliaryAction;
using Game_Fly.GameSound;

namespace Game_Fly.Code
{
    public class Plane
    {
        //1.字段变量：xy坐标，小兵背景，boss背景，游戏阶段，背景移动速度
        int x;
        int y;
        Image image;
        int speed;//游戏速度
        int width;
        int height;
        MoveDirection xMove;
        MoveDirection yMove;
        Image BackgroundImage;
        int hitNumber;//分数
        int grade;//等级
        int life;//玩家生命
        bool isOver;//游戏是否结束
        bool pause;//暂停
        bool bulletType;//真是子弹，假是子弹1[子弹类型]
        //2.属性方法
        public int X
        {
            set { x = value; }
            get { return x; }
        }
        public int Y
        {
            set { y = value; }
            get { return y; }
        }
        public int HitNumber
        {
            set { hitNumber = value; }
            get { return hitNumber; }
        }
        public int Grade
        {
            set { grade = value; }
            get { return grade; }
        }
        public int Life
        {
            set { life = value; }
            get { return life; }
        }
        public int Width
        {
            set { width = value; }
            get { return width; }
        }
        public int Height
        {
            set { height = value; }
            get { return height; }
        }
        public bool IsOver
        {
            set { isOver = value; }
            get { return isOver; }
        }
        public bool Pause
        {
            set { pause = value; }
            get { return pause; }
        }
        /// <summary>
        /// 子弹类型
        /// </summary>
        public bool BulletType
        {
            set { bulletType = value; }
            get { return bulletType; }
        }
        //3.构造方法
        public Plane()
        {
            life = 500;
            hitNumber = 200;
            grade = 1;
            image = GameImage.My_Fly;
            BackgroundImage = GameImage.BJ1;
            width = image.Width;
            height = image.Height;
            x = ((GameUI.GameForm.ClientSize.Width - BackgroundImage.Width) / 2) + (BackgroundImage.Width - width) / 2;
            y = GameUI.GameForm.ClientSize.Height - height - ((height / 2) - 5);
            speed = 7;//移动速度
            isOver = false;
            pause = true;
            bulletType = true;
            //给窗体，添加键盘事件
            GameUI.GameForm.KeyUp += new System.Windows.Forms.KeyEventHandler(GameForm_KeyUp);
            GameUI.GameForm.KeyDown += new System.Windows.Forms.KeyEventHandler(GameForm_KeyDown);
        }

        //键盘弹起
        void GameForm_KeyUp(object sender, System.Windows.Forms.KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case System.Windows.Forms.Keys.Up:
                case System.Windows.Forms.Keys.Down:
                    yMove = MoveDirection.Stop;
                    break;
                case System.Windows.Forms.Keys.Left:
                case System.Windows.Forms.Keys.Right:
                    xMove = MoveDirection.Stop;
                    break;
                case System.Windows.Forms.Keys.ControlKey:
                    break;
                case System.Windows.Forms.Keys.Enter:
                    if (!GameUI.GamePlane.isOver)//玩家有生命才能暂停或开始
                    {
                        if (pause)
                        {
                            GameUI.Time_Run.Stop();
                            pause = false;
                        }
                        else
                        {
                            GameUI.Time_Run.Start();
                            pause = true;
                        }
                    }
                    else//没有生命时这里只能重玩,,初始化
                    {
                        GameUI.GameBackGround = new BackGround();
                        GameUI.GamePlane = new Plane();
                        GameUI.ListBullets = new List<Bullet>();
                        GameUI.ListEnemy1 = new List<Enemy1>();
                        GameUI.ListEnemy2 = new List<Enemy2>();
                        GameUI.ListEnemyBoss = new List<EnemyBoss>();
                        GameUI.ListBulletsTwo = new List<BulletTwo>();
                        GameUI.ListBulletTwoPrize = new List<BulletTwoPrize>();
                        GameUI.ListLifePrize = new List<LifePrize>();
                        GameUI.Time_Run.Start();
                    }
                    break;
            }
        }

        //键盘按下
        void GameForm_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case System.Windows.Forms.Keys.Up:
                    yMove = MoveDirection.Up;
                    break;
                case System.Windows.Forms.Keys.Down:
                    yMove = MoveDirection.Down;
                    break;
                case System.Windows.Forms.Keys.Left:
                    xMove = MoveDirection.Left;
                    break;
                case System.Windows.Forms.Keys.Right:
                    xMove = MoveDirection.Right;
                    break;
                case System.Windows.Forms.Keys.ControlKey:
                    break;
            }
        }

        //4.特征方法
        public void Draw(Graphics g)
        {
            g.DrawImage(image, x, y);
        }

        Bullet bullet = new Bullet();
        BulletTwo bullettwo = new BulletTwo();
        int lastShootTime = 0;//用于计算最后的开火时间
        int count = 0;
        public void Update(int frame)
        {
            //控制飞行
            switch (xMove)//水平飞行
            {
                case MoveDirection.Left:
                    PlaneDirection.Left(ref x, speed, BackgroundImage);
                    break;
                case MoveDirection.Right:
                    PlaneDirection.Right(ref x, speed, BackgroundImage, width);
                    break;
            }
            switch (yMove)//垂直飞行
            {
                case MoveDirection.Up:
                    PlaneDirection.Up(ref y, speed);
                    break;
                case MoveDirection.Down:
                    PlaneDirection.Down(ref y, speed, height);
                    break;
            }

            //飞机开火
            lastShootTime++;//可记录子弹与子弹的间隔
            //如果是开火，实例化子弹发弹
            if (bulletType && lastShootTime >= bullet.Frequency)
            {
                lastShootTime = 0;
                bullet = new Bullet();//创建一个子弹
                bullet.X = x + (width - bullet.Width) / 2 + 8;
                bullet.Y = y - 12;
                GameUI.ListBullets.Add(bullet);
                new GameSound.GameSoundDX(GameMusic.Zd_Music1).Play();//播放子弹的音乐
            }

            //飞机开火
            lastShootTime++;//可记录子弹与子弹的间隔
            //如果是开火，实例化子弹发弹
            if (!bulletType && lastShootTime >= bullettwo.Frequency)
            {
                count++;
                //这里一次创建两颗子弹
                lastShootTime = 0;
                bullettwo = new BulletTwo();//创建一个子弹
                bullettwo.X = x + (width - bullettwo.Width) / 2 + 8;
                bullettwo.Y = y - 12;
                GameUI.ListBulletsTwo.Add(bullettwo);

                bullettwo = new BulletTwo();//创建一个子弹
                bullettwo.X = x + (width - bullettwo.Width) / 2 + 20;
                bullettwo.Y = y - 12;
                GameUI.ListBulletsTwo.Add(bullettwo);
                new GameSound.GameSoundDX(GameMusic.Zd_Music1).Play();//播放子弹的音乐
            }

            //判断子弹1时间是否已过
            if (count > 1000)
            {
                count = 0;
                bulletType = true;
            }
            grade = hitNumber / 200;//等级
        }
    }
    //移动方向
    public enum MoveDirection
    {
        Stop, Left, Right, Up, Down
    }
}
